package cn.xspace.library.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import cn.xspace.library.R;
import cn.xspace.library.bean.OrgDeptUserBean;
import cn.xspace.library.view.tree.bean.Node;

/**
 * Created by liuchen on 2017-02-09.
 */

public class ChildAddressbookViewHolder extends SubViewHolder<Node> {
    ImageView headImg;
    TextView nameTxt;
    TextView jobTxt;
    CheckBox checkBox;
    boolean isShowCheck = false;
    Context mContext;

    TreeRecycleViewAdapter.IItemClickListener iItemClickListener;


    public ChildAddressbookViewHolder(View view, Context mContext, boolean isShowCheck) {
        super(view);
        convertView = view;
        this.headImg = (RoundAngleImageView) view.findViewById(R.id.img_head);
        this.nameTxt = (TextView) view.findViewById(R.id.txt_name);
        this.jobTxt = (TextView) view.findViewById(R.id.txt_job_flag);
        this.isShowCheck = isShowCheck;
        this.mContext = mContext;
        this.checkBox = (CheckBox) view.findViewById(R.id.check_select);
    }

    public void setiItemClickListener(TreeRecycleViewAdapter.IItemClickListener iItemClickListener) {
        this.iItemClickListener = iItemClickListener;
    }

    private int getPadLeft(int level) {
        if (level == 1) {
            return 50;
        } else if (level > 1) {
            return 50 + (level - 1) * 40;
        }
        return 20;
    }

    @Override
    public void bindHolder(Node node) {
        // 设置内边距
        int padleft = getPadLeft(node.getLevel());
        ViewGroup vg = (ViewGroup) convertView;
        View oneView = vg.getChildAt(0);
        if (oneView instanceof RelativeLayout) {
        } else if (oneView instanceof ImageView) {
        }
        if (oneView != null) {
//            oneView.setPadding(padleft, 0, 10, 0);
            int top = convertView.getPaddingTop();
            int bottom = convertView.getPaddingBottom();
//            oneView.setPadding(padleft + 10, top, 20, bottom);
            convertView.setPadding(padleft + 10, top, 20, bottom);
        } else {
            convertView.setPadding(padleft, 3, 3, 3);
        }

        OrgDeptUserBean bean = (OrgDeptUserBean) node.getObject();
        if (isShowCheck) {
            this.checkBox.setVisibility(View.VISIBLE);
            if (bean.isSelectd) {
                this.checkBox.setChecked(true);
            } else {
                this.checkBox.setChecked(false);
            }
            this.checkBox.setEnabled(bean.isCheckBoxEnable);
            if (bean.isDeptAllUser) {
                this.headImg.setVisibility(View.GONE);
            } else {
                this.headImg.setVisibility(View.VISIBLE);
            }

//                if (user_id.equals(bean.user_id)) {
//                    this.checkBox.setVisibility(View.INVISIBLE);
//                }
        }
        this.nameTxt.setText(bean.dept_name);
        if (bean.isGroupChat) { //是否群组,目前已无用
            this.headImg.setImageResource(R.drawable.icon_default_org);
        } else {
            HeadShowUtil.headShow(mContext, bean.user_logo, bean.user_sex, this.headImg, HeadShowUtil.HeadType
                    .UserLogo);
        }

        if ("0".equals(bean.isMainDept)) { //是否兼职
            this.jobTxt.setVisibility(View.VISIBLE);
        } else {
            this.jobTxt.setVisibility(View.GONE);
        }
    }
}
